#include <iostream>
#include <vector>
#include <algorithm>
#include "TreeNode.h"
using namespace std;

ListNode *reverseList(ListNode *head, int length)
{
    ListNode *past = head, *cur = head->next;
    while (cur && length-- > 0)
    {
        ListNode *next = cur->next;
        cur->next = past;
        past = cur;
        cur = next;
    }
    return past;
}

ListNode *reverseBetween(ListNode *head, int left, int right)
{
    ListNode *dummyNode = new ListNode(0, head);
    ListNode *pre = dummyNode, *cur = head;
    while (cur && --left != 0)
    {
        cur = cur->next;
        pre = pre->next;
        --right;
    }
    ListNode *head1 = cur;
    printf("head1 = %d\n", head1->val);
    int k = 0;
    while (cur && --right != 0)
    {
        cur = cur->next;
        k++;
    }
    ListNode *nxt = cur->next;
    printf("nxt = %d\n", nxt->val);
    ListNode *newHead1 = reverseList(head1, k);
    printf("newHead1 = %d\n", newHead1->val);
    pre->next = newHead1;
    head1->next = nxt;
    return dummyNode->next;
}
int main()
{
    ListNode *L5 = new ListNode(5);
    ListNode *L4 = new ListNode(4, L5);
    ListNode *L3 = new ListNode(3, L4);
    ListNode *L2 = new ListNode(2, L3);
    ListNode *L1 = new ListNode(1, L2);
    ListNode *res = reverseBetween(L1, 2, 4);
    for (int i = 0; i < 5; i++)
    {
        printf("res->val = %d\n", res->val);
        res = res->next;
    }
    return 0;
}